package com.example.shell.console;

public class ConsoleTable {
    public static void main(String[] args) {
        // 示例数据（序号，工号，日期，上班时间，下班时间）
        String[][] data = {
            {"1", "E001", "2025-04-16", "09:00", "18:00"},
            {"2", "E002", "2025-04-16", "09:15", "18:30"},
            {"3", "E003", "2025-04-16", "08:50", "17:45"}
        };

        // 列宽定义
        int[] columnWidths = {6, 8, 12, 10, 10}; // 序号(6), 工号(8), 日期(12), 上班(10), 下班(10)

        // 打印表头
        printRow(columnWidths, "序号", "工号", "日期", "上班时间", "下班时间");
        printHorizontalLine(columnWidths);
        
        // 打印数据行
        for (String[] row : data) {
            printRow(columnWidths, row);
        }
    }

    // 打印横线分隔符
    public static void printHorizontalLine(int[] columnWidths) {
        for (int width : columnWidths) {
            System.out.print("+" + repeat(width + 2,'-'));
        }
        System.out.println('+');
    }

    // 打印一行数据
    public static void printRow(int[] columnWidths, String... cells) {
        for (int i = 0; i < cells.length; i++) {
            String cell = cells[i].length() > columnWidths[i] ?
                          cells[i].substring(0, columnWidths[i]-3) + "..." : 
                          cells[i];
            System.out.print(" | " + String.format("%-" + columnWidths[i] + "s", cell));
        }
        System.out.println(" |");
    }

    private static String repeat(int length,char c){
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append(c);
        }
        return builder.toString();
    }
}